package com.sanploy.manage.controller;

import com.sanploy.common.domain.GoodsAttr;
import com.sanploy.common.vo.BaseException;
import com.sanploy.manage.service.GoodsAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;

/**
 * Created by yingzhi zhu on 2017/9/17.
 * @author yingzhi zhu
 * 后台商品规格请求路由类
 */
@RestController
@RequestMapping("/goods/attr")
public class GoodsAttrController {

    @Autowired
    private GoodsAttrService goodsAttrService;

    /**
     * 根据Id获取某个参数详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public  Object getGoodsAttr(@PathVariable Long id){
        return goodsAttrService.findOneAttr(id);
    }

    /**
     * 根据GoodsId 查找商品分类
     */
    @GetMapping("/goods/{goods_id}")
    public Object getByGoodsId(@PathVariable Long goods_id){
        return goodsAttrService.findGoodsAttrByGoodsId(goods_id);
    }

    /**
     * 修改商品分类信息
     */
    @PostMapping("/{id}")
    public Object updateGoods(GoodsAttr goodsAttr,@PathVariable Long id, @RequestParam String attr_name, @RequestParam Long attr_class,
                              @RequestParam int increase_price, @RequestParam String attr_desc, @RequestParam int attr_number,
                              @RequestParam String attr_img, ServletRequest servletRequest) throws BaseException {
        return goodsAttrService.updateGoodsAttr(id,goodsAttr,servletRequest);
    }

    /**
     * 添加参数分类
     * @param name 参数名
     * @param sort 参数排序
     * @return
     * @throws BaseException
     */
    @PostMapping("/class")
    public Object addGoodAttrClass(@RequestParam String name, @RequestParam(defaultValue = "0") int sort) throws BaseException {
        return goodsAttrService.insertGoodsAttrClass(name, sort);
    }

    /**
     * 获取某个参数分类
     * @param id
     * @return
     */
    @GetMapping("/class/{id}")
    public Object getGoodsAttrClassOne(@PathVariable Long id){
        return goodsAttrService.findOneClass(id);
    }

    /**
     * 获取所有商品参数分类名
     * @return
     */
    @GetMapping("/class/all")
    public Object getGoodsAttrClassAll(){
        return goodsAttrService.findAllAttrClass();
    }

    /**
     * 根据id修改某个参数值
     * @param id
     * @param name 参数名
     * @param sort_order 排序
     * @return
     * @throws BaseException
     */
    @PostMapping("/class/{id}")
    public Object editGoodsAttrClass(@PathVariable Long id, @RequestParam String name, @RequestParam int sort_order) throws BaseException {
        return goodsAttrService.updateAttrClassName(name,sort_order,id);
    }
}
